package com.mobilesales.android.services;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.mobilesales.android.model.documents.Document;
import com.mobilesales.android.model.documents.DocumentType;
import com.mobilesales.android.model.documents.PaymentMethod;
import com.mobilesales.android.model.exceptions.ServiceUnavailableException;

public class DocumentServiceClient extends ServiceClientBase {

        public DocumentServiceClient(Configuration configuration) {
                super(configuration);
        }

        public Document GetDocument(int documentId) throws ServiceUnavailableException{
                Document document = null;
                String url = getMethodURL() + "/" + String.valueOf(documentId);
                document = (Document) get(url, Document.class);
                return document;
        }
        
        public List<Document> GetDocumentList() throws ServiceUnavailableException{
                List<Document> documentList = new ArrayList<Document>();
                List<Object> objectList = null;
                Type listType = new TypeToken<ArrayList<Document>>(){}.getType();
                String url = getMethodURL();
                objectList = getList(url, listType);
                if (objectList != null){
                        for(Object obj : objectList){
                                documentList.add((Document)obj);
                        }
                } else 
                        return null;
                return documentList;
        }
        
        public void StoreDocument(Document document) throws ServiceUnavailableException{
                String url = getMethodURL();
                post(url, document);
        }
        
        public Document CreateDocumentFromOrder(Integer orderId) throws ServiceUnavailableException{
                Document document = null;
                String url = getMethodURL();
                document = (Document) postReturn(url, orderId, Document.class);
                return document;
        }
        
        public void Pay(Integer documentId) throws ServiceUnavailableException{
                String url = getMethodURL();
                post(url, documentId);
        }
        
        PaymentMethod GetPaymentMethod(int paymentMethodId) throws ServiceUnavailableException{
                PaymentMethod paymentMethod = null;
                String url = getMethodURL() + "/" + String.valueOf(paymentMethodId);
                paymentMethod = (PaymentMethod) get(url, PaymentMethod.class);
                return paymentMethod;
        }
        
        public List<PaymentMethod> GetPaymentMethodList() throws ServiceUnavailableException{
                List<PaymentMethod> paymentMethodList = new ArrayList<PaymentMethod>();
                List<Object> objectList = null;
                Type listType = new TypeToken<ArrayList<PaymentMethod>>(){}.getType();
                String url = getMethodURL();
                objectList = getList(url, listType);
                if (objectList != null){
                        for(Object obj : objectList){
                                paymentMethodList.add((PaymentMethod)obj);
                        }
                } else 
                        return null;
                return paymentMethodList;
        }
        
        public DocumentType GetDocumentType(int documentTypeId) throws ServiceUnavailableException{
                DocumentType documentType = null;
                String url = getMethodURL() + "/" + String.valueOf(documentTypeId);
                documentType = (DocumentType) get(url, DocumentType.class);
                return documentType;
        }
        
        public List<DocumentType> GetDocumentTypeList() throws ServiceUnavailableException{
                List<DocumentType> documentTypeList = new ArrayList<DocumentType>();
                List<Object> objectList = null;
                Type listType = new TypeToken<ArrayList<DocumentType>>(){}.getType();
                String url = getMethodURL();
                objectList = getList(url, listType);
                if (objectList != null){
                        for(Object obj : objectList){
                                documentTypeList.add((DocumentType)obj);
                        }
                } else 
                        return null;
                return documentTypeList;
        }
}
